# -*- coding: utf-8 -*- 

import datetime
import logging
import urllib2

from google.appengine.ext import db
from BeautifulSoup import BeautifulSoup

from model import *

def update_site_sold_total():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos
    
    for group_info in today_groups:
        update_group_sold_total(group_info)
        

def update_group_sold_total(group_info):
    page = urllib2.urlopen(group_info.link)
    soup = BeautifulSoup(page)
    
    main_div = soup.findAll('div', attrs={'class':'t_area_out_main'})[0]
    
    sold_total = main_div.findAll('div', attrs={'id':'tuanState'})[0].findAll('span')[0].getText()
        
    group_info.sold_total = int(sold_total)
    group_info.put()

    logging.info("groupinfo updated succeed : %s"%group_info.link)


def get_site():
    group_site = GroupSite.get_by_key_name("SITE_aibang")
    if group_site is None:
        group_site = GroupSite(key_name="SITE_aibang")
        group_site.code = "aibang"
        group_site.name = "爱帮团".decode('utf8')
        group_site.link = db.Link("http://tuan.aibang.com/beijing/")
        group_site.pop = 50
        group_site.put()
    
    return group_site

def get_data():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos
    
    if today_groups.count() == 0:
        
        # get site content
        page = urllib2.urlopen(group_site.link)
        
        # soup
        soup = BeautifulSoup(page)
        
        items = soup.findAll('div', attrs={'class' : 't_area_out_main'})
        i = 0
        for item in items:
            title = item.findAll('table', attrs={'class':'at_jrat'})[0].findAll('h1')[0].contents[1]
            title = title.replace("\"", "")

            picture = item.findAll('div', attrs={'class':'t_deal_r'})[0].findAll('img')[0]['src']

            deal_div = item.findAll('div', attrs={'class':'t_deal'})[0]

            current_price = deal_div.findAll('div', attrs={'class':'at_buy'})[0].getText()[1:]

            deal_table = deal_div.findAll('table')[0]

            deal_tr = deal_table.findAll('tr')[1]

            discount = deal_tr.findAll('th')[0].getText()

            original_price = deal_tr.findAll('th')[1].getText()[1:]
                
            link = item.findAll('div', attrs={'class':'t_deal_r'})[0].findAll('a')[0]['href']

            sold_total = item.findAll('span', attrs={'id':'curBuyNumber_%s'%i})[0].getText()
            i+=1
        
            # create group_info
            group_info = GroupInfo()
            group_info.site = group_site
            group_info.title = title
            group_info.current_price = float(current_price)
            group_info.original_price = float(original_price)
            group_info.discount = float(discount)
            group_info.picture = db.Link(picture)
            group_info.link = db.Link(link)
            group_info.sold_total = int(sold_total)
            group_info.create()
            
            group_infos.append(group_info)
    else:
        for group in today_groups:
            group_infos.append(group)
        
    return group_infos


